package com.poetry.vup.works.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.poetry.vup.common.entity.works.VupExhibitionDO;
import com.poetry.vup.common.exception.KabobExceptionUtil;
import com.poetry.vup.common.result.BaseResult;
import com.poetry.vup.common.result.ResultCode;
import com.poetry.vup.common.result.page.BasePage;
import com.poetry.vup.common.util.JacksonUtil;
import com.poetry.vup.user.domain.VupUserDTO;
import com.poetry.vup.user.domain.VupUserVO;
import com.poetry.vup.user.service.VupUserAuthService;
import com.poetry.vup.works.domain.VupDetialVO;
import com.poetry.vup.works.domain.VupExhibitionVO;
import com.poetry.vup.works.mapper.VupExhibitionMapper;
import com.poetry.vup.works.service.VupExhibitionService;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.BreakIterator;
import java.util.*;

@RestController
@RequestMapping("/vup")
@Slf4j
@CrossOrigin
public class VupExhibitionController {

    @Autowired
    private VupExhibitionService vupExhibitionService;

    @Autowired
    private VupExhibitionMapper vupExhibitionMapper;


    /**
     * 查看所有的vup
     */
    @PostMapping("/findVup")
    public BaseResult findVup(@RequestParam("pageSize") int pageSize, @RequestParam("pageNum")int pageNum,
                               @RequestParam(required = false,value = "name") String name){
        BaseResult baseResult = BaseResult.failed("查看失败");
        BasePage<VupExhibitionVO> voPage = new BasePage<>();
        BasePage<VupExhibitionVO> page = vupExhibitionService.findAllVup(pageSize,pageNum);
        voPage.setList(page.getList());
        voPage.setPageNum(page.getPageNum());
        voPage.setPageSize(page.getPageSize());
        voPage.setTotal(page.getTotal());
        voPage.setTotalPage(page.getTotalPage());
        baseResult = BaseResult.success(voPage);
        return baseResult;
    }

    @RequestMapping(value = "/getList",method = RequestMethod.GET)
    public BaseResult findArrayVup(){
        BaseResult baseResult = BaseResult.failed("暂无数据");

        List<Set<VupExhibitionVO>> list = new ArrayList<>();
        Set<VupExhibitionVO> set = new HashSet<>();
        List<VupExhibitionVO> vupExhibitionVOList = vupExhibitionService.findList();
        if(vupExhibitionVOList != null && vupExhibitionVOList.size() > 0){
            for(VupExhibitionVO exhibitionVO : vupExhibitionVOList){
                if(set.size() == 21){
                    break;
                }
                set.add(exhibitionVO);
            }

            baseResult = BaseResult.success(set);
        }

        return baseResult;
    }


    /**
     * 添加vup
     */
    @PostMapping("/addVup")
    public BaseResult addVup(@RequestParam("midId") String midId){
        BaseResult baseResult = BaseResult.failed("获取失败");
        if(StringUtils.isBlank(midId)){
            return BaseResult.failed("输入参数不全");
        }
        QueryWrapper<VupExhibitionDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mid_id",midId);
        VupExhibitionDO vupExhibitionDO = vupExhibitionMapper.selectOne(queryWrapper);
        if(vupExhibitionDO != null){
            return BaseResult.failed("vup已存在，请勿重复添加");
        }
        Long vup = vupExhibitionService.addVup(midId);
        if(vup>=1){
            baseResult = BaseResult.success(vup);
        }
        return baseResult;
    }


    /**
     * 上传图片
     */
    @PostMapping("/uploadVupImage")
    public BaseResult uploadVupImage(@RequestParam("file") MultipartFile file, @RequestParam("id") Long id){
        BaseResult<Object> baseResult = BaseResult.failed("");
        if(id == null){
            baseResult = BaseResult.failed("输入参数不全，请检查");
        }else {
            baseResult = vupExhibitionService.uploadVupImage(file,id);
        }

        return baseResult;
    }

    /**
     * 查看vup详情
     */
    @RequestMapping(value = "/findDetail/{id}",method = RequestMethod.GET)
    public BaseResult findDetail(@PathVariable("id") Long id){
        BaseResult<Object> baseResult = BaseResult.failed("查找失败");
        VupDetialVO vupDetialVO = vupExhibitionService.findDetail(id);
        if(vupDetialVO != null){
            baseResult = BaseResult.success(vupDetialVO);
        }

        return baseResult;
    }

    /**
     * 首页模糊查找
     */
    @RequestMapping(value = "/findVupName",method = RequestMethod.GET)
    public BaseResult findVupName(@RequestParam("name") String name){
        BaseResult<Object> baseResult = BaseResult.failed("查找失败");
        List<VupExhibitionVO> vupExhibitionVOS = vupExhibitionService.findVupName(name);
        baseResult = BaseResult.success(vupExhibitionVOS);
        return baseResult;
    }

    /**
     * 轮播展示
     */
    @RequestMapping(value = "/bannerShow",method = RequestMethod.POST)
    @Scheduled(cron = "0  0  12  *  *  ?")
    public BaseResult bannerShow(){
        BaseResult<Object> baseResult = BaseResult.failed("查找失败");
        //vupExhibitionService.findBanner();
        System.out.println("1111111");
        return baseResult;
    }
}
